<?php
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/Lib/Base.php';
require_once __DIR__ . '/funcs.php';
require_once __DIR__ . '/Lib/Page.php';

checkLogin();
checkFunc(8);

$now = time();
$success = $_GET['success'] ?? null;
$error = $_GET['error'] ?? null;
$action = $_GET['action'] ?? null;

if($action == 'promptly') {
    checkFunc(9, '权限不足，无法操作');
    $arr = [
        ['table' => 'exchange_type', 'table_test' => 'exchange_type_test'],
        ['table' => 'exchange_type_children', 'table_test' => 'exchange_type_children_test'],
        ['table' => 'exchanges', 'table_test' => 'exchanges_test'],
        ['table' => 'game_banner', 'table_test' => 'game_banner_test'],
        ['table' => 'game_config', 'table_test' => 'game_config_test'],
        ['table' => 'game_icon', 'table_test' => 'game_icon_test'],
        ['table' => 'game_sign', 'table_test' => 'game_sign_test'],
        ['table' => 'game_sign_coupon', 'table_test' => 'game_sign_coupon_test'],
        ['table' => 'game_tasks', 'table_test' => 'game_tasks_test'],
    ];
    foreach ($arr as $val){
        $DB->query('truncate table '.$val['table'])->fetchAll();
        $DB->query('insert into '.$val['table'].' select * from '.$val['table_test'])->fetchAll();
    }
    //清空版本历史
    $DB->query('truncate table game_version_log')->fetchAll();
    //将key_test的值复制进key中
    $key_arr = [
        ['key' => 'BANNER_DATA', 'key_test' => 'BANNER_DATA_TEST'],
        ['key' => 'CONFIG_DATA', 'key_test' => 'CONFIG_DATA_TEST'],
        ['key' => 'SIGN_DATA', 'key_test' => 'SIGN_DATA_TEST'],
        ['key' => 'TASK_DATA', 'key_test' => 'TASK_DATA_TEST'],
    ];
    foreach ($key_arr as $v){
        $redis_test = $REDIS->hGetAll($v['key_test']);
        if ($redis_test){
            $REDIS->del($v['key']);
            $REDIS->hMSet($v['key'],$redis_test);
        }
    }
    goExit('game_version.php?success=发布成功!');
}
else if($action == 'timing'){
    checkFunc(9, '权限不足，无法操作');
    $time = $_GET['time'] ?? null;
    echo '<pre>';
    echo $time;die;
}

display_header();
?>
<link href="css/jquery.dm-uploader.min.css" rel="stylesheet">
<style>

</style>

<section class="content">

    <?php display_title('版本管理'); ?>

    <?php if($error) : ?>
        <div class="alert alert-warning alert-dismissible">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
            <span><?php echo $error; ?></span>
        </div>
    <?php endif ?>
    <?php if($success) : ?>
        <div class="alert alert-success alert-dismissible">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
            <h4><i class="icon fa fa-check"></i> 成功</h4>
            <?php echo $success; ?>
        </div>
    <?php endif ?>

    <div class="box box-warning">
        <div class="box-header with-border">
            <h3 class="box-title"><i class="fa fa-tasks"></i> 版本管理</h3>
        </div>
        <div class="box-body">
            <table id="logtable" class="table table-bordered table-striped dataTable table-hover table-condensed" role="grid" aria-describedby="example1_info">
<!--                <thead>-->
<!--                <tr>-->
<!--                    <th class="center" width="230">参数名</th>-->
<!--                    <th class="center" width="300">参数介绍</th>-->
<!--                    <th class="center">操作</th>-->
<!--                </tr>-->
<!--                </thead>-->
                <tbody>
                    <tr role="row" style="height:38px;text-align:center;">
                        <td>
                            <button class="btn btn-primary btn-sm see_edit">查看修改</button>
                        </td>
                        <td>
                            <button class="btn btn-primary btn-sm promptly">立即发布</button>
                        </td>
<!--                        <td style="width: 120px">选择发布时间：</td>-->
<!--                        <td style="width: 100px"><input type="text" style="width:100px;display:inline-block;" name="time" id="time" class="form-control"-->
<!--                                                 value="" readonly /></td>-->
<!--                        <td class="left" style="padding:0;padding-left:15px;">-->
<!--                            <button class="btn btn-primary btn-sm timing">定时发布</button>-->
<!--                        </td>-->
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

</section>

<!-- jQuery 3 -->
<script src="js/jquery.min.js"></script>
<script src="js/jquery.cookie.js"></script>
<script src="js/pace.js"></script>
<!-- Bootstrap 3.3.7 -->
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- Select2 -->
<script src="bower_components/select2/dist/js/select2.full.min.js"></script>
<!-- InputMask -->
<script src="plugins/input-mask/jquery.inputmask.js"></script>
<script src="plugins/input-mask/jquery.inputmask.date.extensions.js"></script>
<script src="plugins/input-mask/jquery.inputmask.extensions.js"></script>
<!-- date-range-picker -->
<script src="bower_components/moment/min/moment.min.js"></script>
<script src="bower_components/bootstrap-daterangepicker/daterangepicker.js"></script>
<!-- SlimScroll -->
<script src="bower_components/jquery-slimscroll/jquery.slimscroll.min.js"></script>
<!-- iCheck 1.0.1 -->
<script src="plugins/iCheck/icheck.min.js"></script>
<!-- FastClick -->
<script src="bower_components/fastclick/lib/fastclick.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/adminlte.min.js"></script>
<script type="text/javascript">
    $(function() {
        var locale = {
            "format": 'YYYY-MM-DD',
            "separator": " -222 ",
            "applyLabel": "确定",
            "cancelLabel": "取消",
            "fromLabel": "起始时间",
            "toLabel": "结束时间'",
            "customRangeLabel": "自定义",
            "weekLabel": "W",
            "daysOfWeek": ["日", "一", "二", "三", "四", "五", "六"],
            "monthNames": ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
            "firstDay": 1
        };
        $('input[name="time"]').daterangepicker({
            locale: locale,
            singleDatePicker: true,
            showDropdowns: true,
            minYear: 1901,
            maxYear: parseInt(moment().format('YYYY'),10)
        }, function(end) {
            //$('#search_end_date').val(end.format('YYYY-MM-DD'));
        });

        //立即发布
        $('.promptly').click(function() {
            if(confirm('确认立即发布吗？\n\n！！！！！ 该操作会清空修改记录,影响线上环境 ！！！！！\n\n')) {
                location.href = 'game_version.php?action=promptly';
            }
        });
        $('.timing').click(function() {
            let time = $('#time').val();
            if(confirm('确认定时发布吗？\n\n！！！！！ 该操作会清空修改记录,影响线上环境 ！！！！！\n\n')) {
                location.href = 'game_version.php?action=timing&time='+time;
            }
        });
        $('.see_edit').click(function() {
            location.href = 'game_data.php';
        });


        setTimeout(function() {
            $('.alert-success').fadeOut();
            if($('.alert-warning').find('span').html() && $('.alert-warning').find('span').html().length < 60) {
                $('.alert-warning').fadeOut();
            }
        }, 2000);
    });
</script>

</body>
</html>
